package com.loongson;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
// import javax.ws.rs.core.MediaType;
import javax.ws.rs.PathParam;
import javax.ws.rs.QueryParam;
// import javax.ws.rs.HeaderParam;
import javax.ws.rs.POST;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.ResponseBuilder;
import javax.ws.rs.core.Response.Status;



import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;


@Path("/cloudservice")
public class RestClient{
    private static final String CLASS_NAME = RestClient.class.getName();
    private static Logger logger = LoggerFactory.getLogger(App.class);

    private Response createResponse(final Status status, final String  payload, final Map<String, String> headers) {
        ResponseBuilder builder = Response.status(status);
        if (headers != null) {
            final  Iterator<String> headerNames = headers.keySet().iterator();
            while (headerNames.hasNext()) {
                final String key = headerNames.next();
                final String value = headers.get(key);
                builder = builder.header(key, value);
             }
        }
       
        Response response = null;
   
        if (payload == null || payload.length() == 0) {
            response = builder.build();
            
        }
        else {
            response = builder.entity(payload).type("application/json").build();
            logger.info(response.toString());
            logger.info("response content:"+response.getEntity().toString());
        }
          return response;
    }

    
    @GET
    @Path("/v1.0/AppList")
    @Produces({ "application/json" })
    public Response getVm(
            @Context final HttpServletRequest httpRequest, 
            @Context final HttpServletResponse httpResponse
            ) throws Exception {

        final String METHOD = "AppList";
        logger.info("Entring class {} method {}",CLASS_NAME,METHOD);
 
        final Map<String, String> headers = new HashMap<String, String>();
        headers.put("Content-Type", "application/json");
        AppUtils appUtils = new AppUtils();
        List<ApplicationLocal> ret = appUtils.scanStartMenuShortcuts();
        JSONObject obj = new JSONObject();
        obj.put("data", ret);
        Response  response = this.createResponse(Status.OK, obj.toJSONString(), headers);
       
        return response;
    }

}